<?php

namespace app\common\model;

use think\Model;

/**
 * Gift
 */
class GiftCode extends Model
{
    // 表名
    protected $name = 'gift_code';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    protected static function onBeforeInsert($model): void
    {
        $pk         = $model->getPk();
        $model->$pk = \app\common\library\SnowFlake::generateParticle();
    }

    protected static function onAfterRead($model):void
    {
        if($model['status'] == 1 && $model['create_time'] < (time()-300)){
            $model->status = 3;
            $model->save();
        }
    }

    public function getIdAttr($value): string
    {
        return (string)$value;
    }

    public function user(): \think\model\relation\BelongsTo
    {
        return $this->belongsTo(\app\admin\model\User::class, 'user_id', 'id');
    }

}